package com.changgou.util;

import com.changgou.entity.FastDFSFile;
import org.csource.common.MyException;
import org.csource.common.NameValuePair;
import org.csource.fastdfs.*;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * FastDFS文件管理
 *
 * @author gaofeng
 * @date 2025年04月19日 14:50
 */
public class FastDFSUtils {
    /**
     * 加载Tracker链接信息
     */
    static {
        try {
            String fileName = new ClassPathResource("fdfs_client.conf").getPath();
            ClientGlobal.init(fileName);
        } catch (IOException | MyException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 文件上传
     *
     * @param file 上传的文件信息
     * @return 文件访问地址
     */
    public static String upload(FastDFSFile file) throws IOException, MyException {
        StorageClient storageClient = getStorageClient();

        NameValuePair[] metaList = new NameValuePair[1];
        metaList[0] = new NameValuePair("author", file.getAuthor());
        String[] uploaded = storageClient.upload_file(file.getContent(), file.getExt(), metaList);
        return getTrackerInfo() + "/" + uploaded[0] + "/" + uploaded[1];
    }

    /**
     * 获取文件信息
     *
     * @param groupName      文件的组名
     * @param remoteFileName 文件的存储完整名
     * @return 文件信息
     */
    public static FileInfo getFileInfo(String groupName, String remoteFileName) throws IOException, MyException {
        StorageClient storageClient = getStorageClient();
        return storageClient.get_file_info(groupName, remoteFileName);
    }

    /**
     * 文件下载
     *
     * @param groupName      文件的组名
     * @param remoteFileName 文件的存储完整名
     */
    public static void download(String groupName, String remoteFileName) throws IOException, MyException {
        StorageClient storageClient = getStorageClient();
        byte[] bytes = storageClient.download_file(groupName, remoteFileName);
        if (bytes == null || bytes.length == 0) {
            return;
        }

        InputStream inputStream = new ByteArrayInputStream(bytes);
        FileOutputStream fileOutputStream = new FileOutputStream("D:/test.jpg");
        byte[] buffers = new byte[1024];
        while (inputStream.read(buffers) != -1) {
            fileOutputStream.write(buffers);
        }

        fileOutputStream.flush();
        fileOutputStream.close();
        inputStream.close();
    }

    /**
     * 文件删除
     *
     * @param groupName      文件的组名
     * @param remoteFileName 文件的存储完整名
     * @return 成功删除的文件数量
     */
    public static int delete(String groupName, String remoteFileName) throws IOException, MyException {
        StorageClient storageClient = getStorageClient();
        return storageClient.delete_file(groupName, remoteFileName);
    }

    /**
     * 获取Storage信息
     *
     * @return Storage信息
     */
    public static StorageServer getStorageInfo() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getStoreStorage(trackerServer);
    }

    /**
     * 获取当前文件存储的Storage的组的信息
     *
     * @param groupName      文件的组名
     * @param remoteFileName 文件的存储完整名
     * @return Storage组的信息
     */
    public static ServerInfo[] getServerInfo(String groupName, String remoteFileName) throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
    }

    /**
     * 获取Tracker的ip、端口信息
     *
     * @return Tracker的ip、端口信息
     */
    public static String getTrackerInfo() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        return "http://" + trackerServer.getInetSocketAddress().getHostString() + ":" + ClientGlobal.getG_tracker_http_port();
    }

    private static StorageClient getStorageClient() throws IOException {
        return new StorageClient(getTrackerServer(), null);
    }

    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        return trackerClient.getConnection();
    }

    public static void main(String[] args) throws MyException, IOException {
        FileInfo fileInfo = getFileInfo("group1", "M00/00/00/wKgDTWgDXEqASMeiAAByqrYNB5U595.JPG");
        download("group1", "M00/00/00/wKgDTWgDXEqASMeiAAByqrYNB5U595.JPG");
        System.out.println(delete("group1", "M00/00/00/wKgDTWgDXEqASMeiAAByqrYNB5U595.JPG"));
        System.out.println(fileInfo.toString());
        System.out.println(getStorageInfo().getInetSocketAddress());
        System.out.println(getServerInfo("group1", "M00/00/00/wKgDTWgDXEqASMeiAAByqrYNB5U595.JPG"));
        System.out.println(getStorageInfo().getStorePathIndex());
        System.out.println(getStorageInfo().getSocket());
        System.out.println(getTrackerInfo());
    }
}
